package com.his.his_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.his_system.domain.DzmHisRegisteredfee;
import com.his.his_system.domain.DzmHisRegisteredfeeSub;
import com.his.his_system.service.DzmHisRegisteredfeeService;
import com.his.his_system.mapper.DzmHisRegisteredfeeMapper;
import com.his.his_system.service.DzmHisRegisteredfeeSubService;
import com.his.his_system.vo.RegisteredfeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 赵康
 * @description 针对表【dzm_his_registeredfee(挂号费用表)】的数据库操作Service实现
 * @createDate 2023-07-11 09:43:14
 */
@Service
public class DzmHisRegisteredfeeServiceImpl extends ServiceImpl<DzmHisRegisteredfeeMapper, DzmHisRegisteredfee> implements DzmHisRegisteredfeeService {

    @Autowired
    private DzmHisRegisteredfeeMapper mapper;


    @Override
    @Transactional
    public Boolean saveOne(RegisteredfeeVo registeredfee, DzmHisRegisteredfeeSubService service) {

        List<DzmHisRegisteredfeeSub> subReg = registeredfee.getSubReg();
        //        先在父费别中添加       得到ID
        int regid = insertReg(subReg, registeredfee);
//        子费别中添加
        for (DzmHisRegisteredfeeSub sub : subReg) {
            sub.setRegId(regid);
        }
        boolean flag = service.saveBatch(subReg);

        return flag;
    }

    @Override
    @Transactional
    public Boolean removeGroup(Integer regId, DzmHisRegisteredfeeSubService subservice) {
        QueryWrapper<DzmHisRegisteredfeeSub> qr = new QueryWrapper<DzmHisRegisteredfeeSub>().eq("reg_id", regId);
        subservice.remove(qr);
        return mapper.deleteById(regId) == 1;
    }

    /**
     * 修改
     *
     * @param regvo
     * @param subservice
     * @return
     */
    @Override
    public Boolean modify(RegisteredfeeVo regvo, DzmHisRegisteredfeeSubService subservice) {


        boolean reg_id =
                subservice.remove(new UpdateWrapper<DzmHisRegisteredfeeSub>().eq("reg_id", regvo.getRegId()));

        boolean b = subservice.saveOrUpdateBatch(regvo.getSubReg());
//要修改的字段  名称 ，费用，子费用，总费用  子费用总数
//        子费用
        BigDecimal subAmount = getSubAmount(regvo.getSubReg());

        DzmHisRegisteredfee dzmHisRegisteredfee = new DzmHisRegisteredfee();
        dzmHisRegisteredfee.setRegId(regvo.getRegId());
        dzmHisRegisteredfee.setRegisteredfeeSubFee(subAmount);
        dzmHisRegisteredfee.setRegisteredfeeFee(regvo.getRegisteredfeeFee());//费别
        dzmHisRegisteredfee.setRegisteredfeeName(regvo.getRegisteredfeeName());//费别名称
        dzmHisRegisteredfee.setNumberofsub(regvo.getSubReg().size());//子费别数量
        assert subAmount != null;
        dzmHisRegisteredfee.setRegisteredfeeAggregateAmount(subAmount.add(regvo.getRegisteredfeeFee()));
        return mapper.updateById(dzmHisRegisteredfee) == 1;
    }

    //   子费用总金额
    private BigDecimal getSubAmount(List<DzmHisRegisteredfeeSub> subregs) {
        BigDecimal count = new BigDecimal(0);
        for (DzmHisRegisteredfeeSub dzmHisRegisteredfeeSub : subregs) {
            count = count.add(dzmHisRegisteredfeeSub.getSubRegisteredfeeFee());
        }
        return count;
    }

    /**
     * 父表插入
     *
     * @param subReg
     * @param registeredfee
     * @return
     */
    private int insertReg(List<DzmHisRegisteredfeeSub> subReg, RegisteredfeeVo registeredfee) {
        BigDecimal count = getSubAmount(subReg);
        //        父费别
        DzmHisRegisteredfee dzmHisRegisteredfee = new DzmHisRegisteredfee();
        //TODO 到时候修改成登录用户中获取
        dzmHisRegisteredfee.setMid(1);
        dzmHisRegisteredfee.setRegisteredfeeSubFee(count);
        dzmHisRegisteredfee.setCompanyId(1); //公司ID
        dzmHisRegisteredfee.setRegisteredfeeFee(registeredfee.getRegisteredfeeFee());//费别
        dzmHisRegisteredfee.setRegisteredfeeName(registeredfee.getRegisteredfeeName());//费别名称
        dzmHisRegisteredfee.setCreateTime(System.currentTimeMillis() / 1000);//创建时间
        dzmHisRegisteredfee.setNumberofsub(subReg.size());//子费别数量
        assert count != null;
        dzmHisRegisteredfee.setRegisteredfeeAggregateAmount(count.add(registeredfee.getRegisteredfeeFee()));
        int insert = mapper.insert(dzmHisRegisteredfee);
        return dzmHisRegisteredfee.getRegId();
    }
}




